<?php
/**
 * Created by IntelliJ IDEA.
 * User: ahwwl
 * Date: 2018/4/17
 * Time: 下午7:35
 */
namespace commen;

use think\Exception;

class bjphbj
{
        private static $url = "https://oli.bjphbj.com:8080/";
    private static $sn = 'a87ff679a2f3e71d9181a67b7542122c';
    private static $key = 'VabltC!DQ(]-&E1ynwH+P$g+UqrR/R<Qd&Ai8(HZKF@/A2m93V=Su2YRV==I$JN6R.yu^6xu+s]C8o3G[}*gyXb]J[YEji$&!(,NF8qC}Nq!XigJ1U>jE}gA3^U,#t0J';
//正式
//    private static $url = "https://oli.bjphbj.com/";
//    private static $sn = '6364d3f0f495b6ab9dcf8d3b5c6e0b01';
//    private static $key = 'KCHM@F*d/a#u0<QL@P7:PXXqO-v+$V(LUw7EHW8w]9(B/joG*<91a&2OHo0bze/_y+:.dy3ay(|q5ZR<3>J.c,j|h<cJ-rE5qX_DYN]jk(oZwP+@cs*L:sNi@+z3SjFu';

    static function get_goods()
    {
        $info = ["error" => 1, "error_info" => "", "data" => []];
        $result = self::get_curl("/open/goods", "");
        if ($result["code"] == 200) {
            $info["error"] = 0;
            $info["data"] = json_decode($result["body"], true);
        } else {
            $info["error_info"] = $result["body"];
        }
        return $info;
    }

    static function get_info()
    {
        $info = ["error" => 1, "error_info" => "", "data" => []];
        $result = self::get_curl("/open/info", "");
        if ($result["code"] == 200) {
            $info["error"] = 0;
            $info["data"] = json_decode($result["body"], true);
        } else {
            $info["error_info"] = $result["body"];
        }
        return $info;
    }

    static function query($order_no)
    {
        $info = ["error" => 1, "error_info" => "", "data" => []];
        $result = self::get_curl("/open/query", json_encode(["tradeNO" => $order_no]));
        if ($result["code"] == 200) {
            $info["error"] = 0;
            $info["data"] = json_decode($result["body"], true);
        } else {
            $info["error_info"] = $result["body"];
        }
        return $info;
    }

    static function recharge($goods_id, $card_no, $order_no, $notifyURL = "")
    {
        $info = ["error" => 1, "error_info" => "", "data" => []];
        $result = self::get_curl("/open/recharge",
            json_encode([
                "goodsID" => $goods_id,
                "cardNO" => $card_no,
                "tradeNO" => $order_no,
                "notifyURL" => $notifyURL
            ]));
        if ($result["code"] == 202) {
            $info["error"] = 0;
        } else {
            $info["error_info"] = $result["body"];
        }
        $info["code"] = $result["code"];
        return $info;
    }

    static function callback()
    {
        $info = ["error" => 1, "error_info" => "", "data" => []];
        $raw_post_data = file_get_contents('php://input', 'r');
        try {
            $raw_post_data = json_decode($raw_post_data, true);
            if (empty($raw_post_data))
                throw new Exception("数据为空");
            $order_no = $raw_post_data["tradeNO"];
            if (empty($order_no))
                throw new Exception("没有订单号");
            $query_result = self::query($order_no);
            if (empty($query_result) || $query_result["error"] == 1)
                throw new Exception("查询信息失败");
            $state = $query_result["data"]["state"];
            $info["data"]["supply_basic_price"] = intval( $query_result["data"]["price"])*100;
            $info["data"]["supply_cost"] = intval( $query_result["data"]["actualPrice"])*100;
            $info["data"]["order_no"] = $order_no;
            if (in_array($state, ["notifying", "complete"])) {
                $info["data"]["status"] = 2;
            } elseif (in_array($state, ["refund"])) {
                $info["data"]["status"] = 3;
            } else {
                $info["data"]["status"] = 1;
            }
            $info["error"] = 0;
        } catch (\Exception $e) {

        }
        return $info;
    }


    static function get_curl($action, $body)
    {
        $headers = ["Authorization: " . self::$sn . " " . md5($body . self::$key), 'Content-Type: application/json'];
        $url = self::$url . $action;
        return self::curl_url($url, $headers, $body);
    }

    static function curl_url($url, $headers, $body)
    {
        $result = ["code" => 500, "body" => "", "error_info" => ""];
        try {
            $ch = curl_init();
            //设置选项，包括URL
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            if (!empty($headers))
                curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            if ($body) {
                curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
            }
            //执行并获取HTML文档内容
            $output = curl_exec($ch);
            if ($output === false) {
                $result["error_info"] = curl_errno($ch);
            }
            $result["code"] = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            $result["body"] = $output;
            //释放curl句柄
            curl_close($ch);
        } catch (\Exception $e) {
            $result["error_info"] = $e->getMessage();
        }
        return $result;
    }
    static function applyDiese($card_id,$plate_number,$card_img_url_1,$card_img_url_2,$driving_img_url_1,$driving_img_url_2,$notifyURL)
    {
        $info = ["error"=>1,"error_info"=>"","data"=>[]];
        $result = self::get_curl("/open/applyDiese",
            json_encode([
                "CardID"=>$card_id,
                "PlateNumber"=>$plate_number,
                "CardImgURL1"=>$card_img_url_1,
                "CardImgURL2"=>$card_img_url_2,
                "DrivingImgURL1"=>$driving_img_url_1,
                "DrivingImgURL2"=>$driving_img_url_2,
                "NotifyUrl"=>$notifyURL
            ]));
        $body = $result["body"];
        try
        {
            $body = json_decode($body,true);
        }catch (\Exception $e)
        {
            $body = [];
        }

        if($result["code"] == 201 )
        {
            $info["error"] = 0;

        }else{
            if(!empty($body))
            {
                $info["error_info"] = $body["error"];
            }else{
                $info["error_info"] = $result["body"];
            }

        }

        return $info;
    }


}